//
//  ChoosePictureCell.m
//  A段项目
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "ChoosePictureCell.h"

@interface ChoosePictureCell ()



@end

@implementation ChoosePictureCell

{
    
    BOOL selected;
   
    
}


- (void)dealloc {
    
     [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    [_image release];
    [_imageView release];
    [_button release];
    [super dealloc];
    
}

#pragma mark -- 初始化.
- (instancetype) initWithFrame:(CGRect)frame {
   self = [super initWithFrame:frame];
    
    if (self) {
       
       
        self.imageView = [[UIImageView alloc] init];
        
      
        
        [self.contentView addSubview:self.imageView];
        
        self.button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [self.contentView addSubview:self.button];

        
        [self.imageView release];
       
    }
    return self;
}


#pragma mark --布局
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    
    self.imageView.frame = self.bounds;
    
   
    self.imageView.backgroundColor = [UIColor redColor];
    
    self.button.frame = CGRectMake(self.frame.size.width - 20, self.frame.size.height - 20, 20, 20);
    
    [self.button setImage:[UIImage imageNamed:@"ture"] forState:UIControlStateNormal];
    
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)buttonAction:(UIButton *)button {
    
    
    
    if (!selected) {
        
        [self.button setImage:[UIImage imageNamed:@"redTrue"] forState:UIControlStateNormal];
        

        [[NSNotificationCenter defaultCenter] postNotificationName:@"add" object:nil userInfo:@{@"item":self.imageView.image}]; //将已经打勾的照片传过去
        
    }else {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"sub" object:nil userInfo:@{@"item1":self.imageView.image}];
        
        [self.button setImage:[UIImage imageNamed:@"ture"] forState:UIControlStateNormal];
        
        
    }
    

    /**从第五页进入到本页面 button 清零. */
    [[NSNotificationCenter defaultCenter] addObserverForName:@"viewAppeared" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
         [self.button setImage:[UIImage imageNamed:@"ture"] forState:UIControlStateNormal];
        
        
        selected = 0;
        
    }];
  
    
    selected = !selected;
}

- (void)passedInfo:(UIImage *)image {
    
   
        
    self.imageView.image = image;
    
        [self.contentView addSubview:self.imageView];
        
    [self.contentView bringSubviewToFront:self.button];
    
}


@end
